<?php
namespace app\shop\controller;
use app\ApiRest;
use app\shop\model\IndexUserInfo;
use think\App;
use app\shop\model\IndexStore as Model;


class IndexStore extends ApiRest{

    protected $model;
    protected $distrbutor_model;
    public function __construct(App $app) {

        parent::__construct($app);

        $this->model  = new Model();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-23 09:45
     * @功能说明:自提点列表
     */
    public function storeList(){

        $input = $this->_param;
        //经度
        $lng   = $input['longitude'];
        //纬度
        $lat   = $input['latitude'];

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $where = [];

        if(!empty($input['title'])){
            //名字搜索
            $where[] = ['title','like','%'.$input['title'].'%'];
            //地址搜索
            $where[] = ['address','like','%'.$input['title'].'%'];

        }elseif (!empty($input['city'])){

            $where[]  = ['city','like','%'.$input['city'].'%'];

            $where[] = ['province','like','%'.$input['city'].'%'];

            $where[] = ['area','like','%'.$input['city'].'%'];

        }
        //查距离
        $alh = '(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*('.$lng.'- `longitude`)/360),2)+COS(PI()*33.07078170776367/180)* COS('.$lat.' * PI()/180)*POW(SIN(PI()*('.$lat.'- latitude)/360),2)))) as distance';

        $data = $this->model->storeList($dis,$where,10,'*',$alh);

        return $this->success($data);
    }



    /**
     * @author chenniang
     * @DataTime: 2020-06-23 09:45
     * @功能说明:自提点列表
     */
    public function storeListV2(){

        $input = $this->_param;

        $user_info_model = new IndexUserInfo();

        $company_id = $user_info_model->getCompanyId(['fans_id'=>$input['staff_id']],$this->_uniacid);
        //经度
        $lng   = $input['longitude'];
        //纬度
        $lat   = $input['latitude'];

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','=',1];

        $where = [];

        if(!empty($input['title'])){
            //名字搜索
            $where[] = ['a.title','like','%'.$input['title'].'%'];
            //地址搜索
            $where[] = ['a.address','like','%'.$input['title'].'%'];

        }elseif (!empty($input['city'])){

            $where[]  = ['a.city','like','%'.$input['city'].'%'];

            $where[] = ['a.province','like','%'.$input['city'].'%'];

            $where[] = ['a.area','like','%'.$input['city'].'%'];

        }

        $map = [

            'a.public_company' => 1,

            'b.company_id'     => $company_id
        ];
        //查距离
        $alh = '(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*('.$lng.'- `longitude`)/360),2)+COS(PI()*33.07078170776367/180)* COS('.$lat.' * PI()/180)*POW(SIN(PI()*('.$lat.'- latitude)/360),2)))) as distance';

        $data = $this->model->storeListV2($dis,$where,10,'a.*',$alh,$map);

        return $this->success($data);
    }

    /**
     * @author chenniang
     * @DataTime: 2020-06-23 10:09
     * @功能说明:自提点详情
     */
    public function storeInfo(){

        $input = $this->_param;

        $dis   = [

            'id' => $input['id'],

            'status' => 1
        ];

        $data = $this->model->storeInfo($dis);

        if(empty($data)){

            $this->errorMsg('自提点已关闭');
        }

        $order_model = new \app\shop\model\IndexShopOrder();

        $zt_time = $order_model->where(['id'=>$input['order_id']])->value('zt_time');

        if(!empty($zt_time)){

            $zt_time = explode('-',$zt_time);

            $date = date('Y-m-d',$zt_time[0]);

            $week = date('w',$zt_time[0]);

            $week = changeWeek($week);

            $zt_start_time = date('H:i',$zt_time[0]);

            $zt_end_time   = date('H:i',$zt_time[1]);

            $data['zt_time_text'] = $date.'('.$week.')'.' '.$zt_start_time.'-'.$zt_end_time;

        }
        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-23 10:40
     * @功能说明:门店时间（天）
     */
    public function storeDay(){

        $input = $this->_param;

        $dis   = [

            'id' => $input['id'],

            'status' => 1
        ];
        //门店详情
        $data = $this->model->storeInfo($dis);

        if(empty($data)){

            $this->errorMsg('自提点已关闭');
        }
        //最长预约多少天的
        $max_day = $data['max_day'];

        if($max_day<1){

            $this->errorMsg('未设置最长预约天数');

        }

        $date = $this->model->getDayMax($data);

        return $this->success($date);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-23 11:30
     * @功能说明:门店时间（小时）
     */
    public function storeTime(){

        $input = $this->_param;

        $time  = $input['time'];

        $dis   = [

            'id' => $input['id'],

            'status' => 1
        ];
        //门店详情
        $data = $this->model->storeInfo($dis);

        if(empty($data)){

            $this->errorMsg('自提点已关闭');
        }
        $week  = date('w',$time);
        //凌晨
        $time  = strtotime(date('Y-m-d',$time));
        //细分时段
        $time_slot = $data['time_slot']==1?3600:1800;
        //未自定义时间
        if($data['reception_time']==0){

            $all_time = 86400;

            $add_time = 0;

        }else{
            //非工作日
            if(in_array($week,[6,0])){
                //开始时间
                $s_time = strtotime($data['no_work_start_time']);
                //结束时间
                $e_time = strtotime($data['no_work_end_time']);

            }else{
                //开始时间
                $s_time = strtotime($data['work_start_time']);
                //结束时间
                $e_time = strtotime($data['work_end_time']);

            }
            //时间段
            $all_time = $e_time - $s_time;
            //起始时间
            $add_time = $s_time - strtotime(date('Y-m-d',time()));

            $add_time_end = $e_time - strtotime(date('Y-m-d',time()));

        }
        //每一小段段时间
        $time_r = $all_time/$time_slot;

        $arr = [];

        for ($i=0;$i<$time_r;$i++){

            $time_s     = $i*$time_slot+$add_time;
            //开始时间段
            $start_time = date('H:i',$time+$time_s);

            if(!empty($add_time_end)&&$add_time_end<($time_s+$time_slot)){

                $end_time_t = $add_time_end;

            }else{

                $end_time_t = $time_s+$time_slot;
            }
            //结束时间段
            $end_time   = date('H:i',$time+$end_time_t);
            //拼接好的时间
            $arr[$i]['time_text'] = $start_time.'-'.$end_time;
            //时间戳
            $arr[$i]['time_arr']  = ($time+$time_s).'-'.($time+$end_time_t);
            //状态
            $arr[$i]['status']    = 1;
            //延后时间
            $l_time = $time+$end_time_t;

            if($data['delay']==0&&!empty($data['delay_time'])&&$l_time<$data['delay_time']*60+time()){

                $arr[$i]['status']  = 0;

            }
            
            if($time+$end_time_t<time()){

                $arr[$i]['status']  = 0;
            }

        }


        return $this->success($arr);

    }











}
